package com.hmall.item.web;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constant.MqConstant;

import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 分页查询商品
     * @param size
     * @param page
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> list(@RequestParam("size") Integer size,@RequestParam("page") Integer page){
        Page<Item> itemPage = itemService.page(new Page<>(page, size));
        return new PageDTO<>(itemPage.getTotal(),itemPage.getRecords());
    }

    /**
     * 添加商品
     * @param item
     */
    @PostMapping
    public void saveItem(@RequestBody Item item){
        itemService.save(item);

        rabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE,
                MqConstant.HOTEL_INSERT_KEY,
                item.getId());
    }

    /**
     * 删除商品
     * @param id
     */
    @DeleteMapping("{id}")
    public void deleteItem(@PathVariable("id") Long id){
        itemService.removeById(id);
        log.info("删除");

        rabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE,
                MqConstant.HOTEL_DELETE_KEY, id);
    }

    /**
     * 修改商品
     * @param item
     */
    @PutMapping
    public void updateItem(@RequestBody Item item){
        itemService.updateById(item);

        rabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE,
                MqConstant.HOTEL_INSERT_KEY,
                item.getId());
    }

    /**
     * 上架下架商品
     * @param id
     * @param status
     */
    @PutMapping("status/{id}/{status}")
    public void upOrDownItem(@PathVariable Long id
                            ,@PathVariable Integer status){
        if (status == 2) {
            Item itemBuilder = Item.builder().status(2).id(id).build();
            itemService.updateById(itemBuilder);
            log.info("下架");

            rabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE,
                    MqConstant.HOTEL_DELETE_KEY, id);
        }
        if (status == 1) {
            Item itemBuilder = Item.builder().status(1).id(id).build();
            itemService.updateById(itemBuilder);
            log.info("上架");
            rabbitTemplate.convertAndSend(MqConstant.HOTEL_EXCHANGE,
                    MqConstant.HOTEL_INSERT_KEY, id);
        }

    }

    @GetMapping("{id}")
    public Item findItemById(@PathVariable("id") Long id){
        Item item = itemService.getById(id);
        return item;
    }

    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemid")Long itemId
                            ,@PathVariable("num")Integer num){
        Item item = itemService.getById(itemId);
        Integer stock = item.getStock()-num;
        item.setStock(stock);
        itemService.updateById(item);


    }

}
